package com.han.nettytestdemo.connector.communication;

/**
 * Created by bayi on 2016/3/1.
 *  update by xuge on 2019/6/12
 */
public class ConsumerProduct extends Thread {

    private static final String TAG = "ConsumerProduct";
    private OrderStorage orderStorage;
    private ConsumptionOrderInterface usbHostSerial;
    private static ConsumerProduct consumerProduct;

    public static ConsumerProduct getInstance(ConsumptionOrderInterface usbHostSerialImpl, OrderStorage orderStorage) {
        return consumerProduct = consumerProduct == null ? new ConsumerProduct(usbHostSerialImpl, orderStorage) : consumerProduct;
    }

    public ConsumerProduct(ConsumptionOrderInterface usbHostSerialImpl, OrderStorage orderStorage) {
        this.orderStorage = orderStorage;
        this.usbHostSerial = usbHostSerialImpl;
    }

    @Override
    public void run() {
        while (!isInterrupted()) {
            try {
                Order order = orderStorage.takeOrder();
                usbHostSerial.runOrder(order);
            } catch (Exception e) {
                e.printStackTrace();
                interrupt();
            }
        }
    }

}
